/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        return dfs(list1,list2);
    }

    ListNode* dfs(ListNode* n1,ListNode* n2)
    {
        if(n1==nullptr)return n2;
        if(n2==nullptr)return n1;   
        if(n1->val<=n2->val)
        {
            n1->next=dfs(n1->next,n2);
            return n1;
        }
        else
        {
             n2->next=dfs(n1,n2->next);
             return n2;
        }
    } 
};
